<template>
	<view class="wrap">
		<view class="tHeader">
			<view class="search">
				<view class="search-L">
					<u-search placeholder="搜索店内商品" v-model="keyword" height="60" :show-action="false"
						search-icon-color="#b3b3b3" placeholder-color="#b3b3b3" @search="onSearchConfirm"></u-search>
				</view>
				<view class="classify" @click="showClassifyFlag = !showClassifyFlag">
					<text>全部分类</text>
				</view>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="onGoodsItemTap(index)">
				<view class="item-radio">
					<radio color="#3377FF" :checked="item.checked"></radio>
				</view>
				<view class="item-img">
					<image :src="item.cover"></image>
				</view>
				<view class="item-main">
					<view class="item-title">{{item.productName}}</view>
					<view class="item-mainB">
						<view class="item-mainB_L">
							<text>￥{{item.price}}/{{item.unit}}</text>
							<text>（{{item.emptyDiff}}）</text>
						</view>
						<view class="item-mainB_R">货号：{{item.id}}</view>
					</view>
				</view>
			</view>
			<no-more v-if="page > totalPage && hasLoad"></no-more>
			<no-data v-if="hasLoad && list.length == 0"></no-data>
		</view>
		<view class="submitBar">
			<g-btn title="确定报价" :customstyle="{height: '70rpx'}" @click="onConfirmTap"></g-btn>
		</view>
		<!-- 分类选择 -->
		<u-popup v-model="showClassifyFlag" mode="top">
			<view class="classifyPop">
				<scroll-view class="classifyPop-L" scroll-y="true">
					<view class="classifyPop-Litem" :class="{active: item.active}" v-for="(item, index) in typeList"
						:key="index" @click="onTypeItemTap(index)">
						{{item.typeTitle}}
					</view>
				</scroll-view>
				<scroll-view class="classifyPop-R" scroll-y="true">
					<view class="classifyPop-Ritem" :class="{active: item.active}"
						v-for="(item, index) in typeChildrenList" :key="index" @click="onTypeChildrenItemTap(index)">
						<text>{{item.typeTitle}}</text>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				purchaseId: '',
				keyword: '',
				showClassifyFlag: false,
				list: [],
				totalPage: 1,
				page: 1,
				size: 4,
				hasLoad: false,
				noMore: false,
				typeList: [],
				typeChildrenList: []
			}
		},
		onLoad(options) {
			this.purchaseId = options.purchaseId
			this.$store.dispatch('updateUserInfo').then(() => {
				this.updatePage()
			})
		},
		methods: {
			updatePage() {
				this.resetPage()
				this.getList()
				this.getProductTypeList()
				this.getProductTypeChildren()
			},
			resetPage() {
				this.list = []
				this.totalPage = 1
				this.page = 1
				this.size = 15
				this.hasLoad = false
				this.noMore = false
			},
			onSkipTap(type, index) {
				const urlObj = {
					// shopCartSelect: '/pages/commodityManagement/shopCartSelect',
				}
				this.$g_util.navigationTo(urlObj, type)
			},
			onGoodsItemTap(index) {
				this.list.forEach((item, idx) => {
					item.checked = index == idx
				})
			},
			onTypeItemTap(index) {
				this.typeList.forEach((item, idx) => {
					item.active = index == idx
				})
				this.getProductTypeChildren()
			},
			onTypeChildrenItemTap(index) {
				this.typeChildrenList.forEach((item, idx) => {
					item.active = index == idx
				})
				this.showClassifyFlag = false
				this.resetPage()
				this.getList()
			},
			async checkParams() {
				const productId = this.list.find(item => item.checked)?.id
				if(!productId) return Promise.reject('请选择商品')
				return Promise.resolve({
					productId,
					purchaseId: this.purchaseId,
					quotedType: 1,
					// shopId: this.gUserInfo.shopId
				})
			},
			async onConfirmTap() {
				try {
					await this.$g_api.purchaseQuoted(await this.checkParams())
					uni.showToast({
						title: '报价成功'
					})
					uni.$emit('update-purchaseNote')
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				} catch (e) {
					//TODO handle the exception
					console.log(e);
					uni.showToast({
						title: e.toString(),
						icon: 'none'
					})
				}
			},
			getList() {
				if (this.page > this.totalPage) return
				let params = {
					pageNum: this.page,
					pageSize: this.size,
					createUserId: this.gUserInfo.userId
				}
				if (this.keyword) {
					params.productName = this.keyword
				}
				let typeChildrenItem = this.typeChildrenList.find(item => item.active)
				if (typeChildrenItem) {
					params.productTypeId = typeChildrenItem.id
				}
				this.$g_api.productSearchProducts(params).then(({
					rows
				}) => {
					uni.stopPullDownRefresh()
					this.hasLoad = true
					if (rows?.length > 0) {
						rows = this.$g_util.concatRequestUrl(rows, 'cover')
						rows.forEach(item => {
							item.createTimeStr = new Date(item.createTime).Format('yyyy-MM-dd')
							item.checked = false
							// item.secretTel = item.tel.slice(0, 3) + '****' + item.tel.slice(7, 11)
						})
						this.list = this.list.concat(rows)
					} else {
						this.noMore = true
					}
				})
			},
			getProductTypeList() {
				this.$g_api.productTypeList().then(({
					data
				}) => {
					if (data?.length > 0) {
						this.typeList = data.map((item, index) => {
							item.active = index == 0
							return item
						})
						this.getProductTypeChildren()
					}
				})
			},
			getProductTypeChildren() {
				let typeItem = this.typeList.find(item => item.active)
				if (!typeItem) return
				this.$g_api.productTypeChildren({
					parentId: typeItem.id
				}).then(({
					data
				}) => {
					if (data?.length > 0) {
						this.typeChildrenList = data.map((item, index) => {
							item.active = false
							return item
						})
					}
				})
			},
			onSearchConfirm(val) {
				this.keyword = val
				this.resetPage()
				this.getList()
			}
		}
	}
</script>
<style>
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}
</style>
<style scoped lang="scss">
	::v-deep .u-numberbox {
		border: 1px solid #e6e6e6;
		border-radius: 6rpx;
	}

	::v-deep .u-number-input {
		min-width: 82rpx !important;
		margin: 0 !important;
		background-color: #fff !important;
	}

	.wrap {
		padding-top: 80rpx;
	}

	.tHeader {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		background-color: #fff;
		padding-bottom: 20rpx;
		border-bottom: 1px solid $g-border-color;
		z-index: 10086;
	}

	.search {
		display: flex;
		align-items: center;
		padding: 0 30rpx;
	}

	.search-L {
		flex: 1;
		min-width: 300rpx;
	}

	.classify {
		display: flex;
		justify-content: center;
		min-width: 190rpx;
		padding-left: 10rpx;
		box-sizing: border-box;

		text {
			position: relative;

			&::after {
				content: '';
				width: 12rpx;
				height: 12rpx;
				position: absolute;
				right: -30rpx;
				top: 50%;
				border-top: 1px solid #888;
				border-right: 1px solid #888;
				transform: translateY(-50%) rotate(135deg);
			}
		}
	}

	.list {
		padding-bottom: calc(100rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
	}

	.item {
		display: flex;
		padding: 30rpx 30rpx 30rpx 20rpx;
		border-bottom: 1px solid $g-border-color;
		background-color: #fff;
	}

	.item-radio {
		margin-right: 10rpx;
		display: flex;
		align-items: center;

		radio {
			transform: scale(0.75);
		}
	}

	.item-img {
		width: 140rpx;
		font-size: 0;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.item-main {
		flex: 1;
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.item-mainB {
		display: flex;
		justify-content: space-between;
	}

	.item-mainB_L {
		text {
			&:first-child {
				color: #FF4C4C;
				font-size: 26rpx;
			}

			&:last-child {
				color: #999999;
				font-size: 20rpx;
			}
		}
	}

	.item-mainB_R {
		font-size: 24rpx;
		color: #999;
	}

	.submitBar {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100vw;
		height: 70rpx;
		box-sizing: content-box;
		padding-top: 15rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 15rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 15rpx);
		background-color: #fff;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
	}

	.classifyPop {
		display: flex;
		padding-top: 80rpx;
	}

	.classifyPop-L {
		width: 200rpx;
		max-height: 60vh;
		background-color: $g-main-bg;
	}

	.classifyPop-Litem {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 200rpx;
		height: 80rpx;

		&.active {
			background-color: #fff;
		}
	}

	.classifyPop-R {
		width: 550rpx;
		max-height: 60vh;
		padding: 0 30rpx;
	}

	.classifyPop-Ritem {
		display: flex;
		align-items: center;
		height: 80rpx;

		&.active {
			position: relative;
			color: $g-main-color;

			&::after {
				content: '';
				position: absolute;
				right: 2rpx;
				top: 50%;
				width: 26rpx;
				height: 14rpx;
				border-left: 2px solid $g-main-color;
				border-bottom: 2px solid $g-main-color;
				transform: translateY(-60%) rotate(-45deg);
			}
		}
	}

	.optPop {
		position: relative;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
	}

	.optPop-close {
		position: absolute;
		right: 20rpx;
		top: 30rpx;
	}

	.optPop-goods {
		display: flex;
		padding: 30rpx;
		background-color: #fff;
	}

	.optPop-img {
		width: 140rpx;
		font-size: 0;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.optPop-main {
		flex: 1;
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.optPop-title {
		max-width: 480rpx;
		font-size: 28rpx;
		font-weight: bold;
	}

	.optPop-mainB {
		display: flex;
		justify-content: space-between;
	}

	.optPop-mainB_L {
		text {
			&:first-child {
				color: #FF4C4C;
				font-size: 26rpx;
			}

			&:last-child {
				color: #999999;
				font-size: 20rpx;
			}
		}
	}

	.optPop-mainB_R {
		font-size: 24rpx;
		color: #999;
	}

	.optPop-search {
		padding: 0 30rpx;
	}

	.optPop-sHeader {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		height: 60rpx;
		margin: 18rpx 0;
	}

	.optPop-sHeader_L {}

	.optPop-sHeader_R {
		display: flex;
	}

	.optPop-sHeader_btn {
		margin-left: 20rpx;
		width: 120rpx;
		// height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		font-size: 24rpx;
		color: #333;
		background: #F2F2F2;
		border-radius: 30rpx;

		&.active {
			color: $g-main-color;
			border: 1px solid $g-main-color;
			background-color: #fff;
		}
	}

	.optPop-list {
		padding-left: 34rpx;
		max-height: 50vh;
		margin-bottom: 30rpx;
		border-bottom: 1px solid $g-border-color;
	}

	.optPop-item {
		display: inline-block;
		margin-right: 32rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
		background-color: $g-main-bg;
		overflow: hidden;

		&:nth-child(4n) {
			margin-right: 0;
		}

		&.active {
			background-color: #fff;
			border: 1px solid #FF7733;

			.optPop-item_text {
				color: #FF7733;
			}
		}
	}

	.optPop-item_img {
		width: 146rpx;
		font-size: 0;

		image {
			width: 146rpx;
			height: 146rpx;
			// border-radius: 10rpx;
			background-color: #F8C701;
		}
	}

	.optPop-item_text {
		padding: 8rpx 10rpx;
		color: #333;
		font-size: 20rpx;
		text-align: center;
	}

	.optPop-btn {
		margin-top: 40rpx;
	}

	.countJYPop {
		position: relative;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
	}

	.countJYPop-close {
		position: absolute;
		right: 20rpx;
		top: 30rpx;
	}

	.countJYPop-header {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 32rpx;
	}

	.countJYPop-list {
		padding: 30rpx 30rpx 0;
	}

	.countJYPop-item {
		display: flex;
		margin-bottom: 30rpx;
	}

	.countJYPop-img {
		width: 120rpx;
		flex-shrink: 0;
		margin-right: 20rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.countJYPop-main {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		max-width: 340rpx;
	}

	.countJYPop-title {
		// max-width: 340rpx;
	}

	.countJYPop-mainM {
		text {
			&:first-child {
				font-size: 20rpx;
				color: #ff4c4c;
			}

			&:nth-child(2) {
				font-size: 28rpx;
				color: #ff4c4c;
			}

			&:nth-child(3) {
				font-size: 20rpx;
				color: #ff4c4c;
			}

			&:nth-child(4) {
				font-size: 20rpx;
				color: #999;
			}

			&:nth-child(5) {
				margin-left: 20rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	.countJYPop-tips {
		font-size: 24rpx;
		color: #999;
	}

	.countJYPop-R {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
		margin-left: 20rpx;
		flex-shrink: 0;
		max-width: 190rpx;
	}

	.countJYPop-mdPrice {
		padding-right: 26rpx;

		&::after {
			right: 10rpx;
		}
	}

	.countJYPop-total {
		text-align: right;
		padding-right: 30rpx;

		text {
			font-size: 26rpx;
			color: #999;

			&:nth-child(2) {
				margin-left: 10rpx;
				color: #333;
			}

			&:nth-child(3) {
				font-size: 20rpx;
				color: #FF4C4C;
			}

			&:nth-child(4) {
				color: #FF4C4C;
			}

			&:nth-child(5) {
				font-size: 20rpx;
				color: #FF4C4C;
			}
		}
	}

	.countJYPop-btn {
		margin-top: 50rpx;
	}

	.countDHPop {
		position: relative;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
	}

	.countDHPop-close {
		position: absolute;
		right: 20rpx;
		top: 30rpx;
	}

	.countDHPop-header {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 32rpx;
	}

	.countDHPop-list {
		padding: 30rpx 30rpx 0;
		box-sizing: border-box;
	}

	.countDHPop-item {
		display: flex;
		margin-bottom: 30rpx;

		&.app_arrow {
			&::after {
				right: 10rpx;
				width: 14rpx;
				height: 14rpx;
			}
		}
	}

	.countDHPop-img {
		width: 120rpx;
		flex-shrink: 0;
		margin-right: 20rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.countDHPop-main {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		max-width: 380rpx;
	}

	.countDHPop-title {
		// max-width: 340rpx;
	}

	.countDHPop-mainM {
		text {
			&:first-child {
				font-size: 20rpx;
				color: #ff4c4c;
			}

			&:nth-child(2) {
				font-size: 28rpx;
				color: #ff4c4c;
			}

			&:nth-child(3) {
				font-size: 20rpx;
				color: #ff4c4c;
			}

			&:nth-child(4) {
				font-size: 20rpx;
				color: #999;
			}

			&:nth-child(5) {
				margin-left: 20rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	.countDHPop-tips {
		font-size: 24rpx;
		color: #999;
	}

	.countDHPop-R {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;
		margin-left: 20rpx;
		flex-shrink: 0;
		max-width: 190rpx;
	}

	.countDHPop-R_text {
		font-size: 24rpx;
		color: #999;
	}

	.countDHPop-mdPrice {
		padding-right: 26rpx;

		&::after {
			right: 10rpx;
		}
	}

	.countDHPop-total {
		text-align: right;
		padding-right: 30rpx;

		text {
			font-size: 26rpx;
			color: #999;

			&:nth-child(2) {
				margin-left: 10rpx;
				color: #333;
			}

			&:nth-child(3) {
				font-size: 20rpx;
				color: #FF4C4C;
			}

			&:nth-child(4) {
				color: #FF4C4C;
			}

			&:nth-child(5) {
				font-size: 20rpx;
				color: #FF4C4C;
			}
		}
	}

	.countDHPop-btn {
		margin-top: 50rpx;
	}

	.mdyPricePop {
		width: 600rpx;
	}

	.mdyPricePop-header {
		line-height: 100rpx;
		font-size: 36rpx;
		color: #333;
		text-align: center;
	}

	.mdyPricePop-main {
		padding: 0 50rpx 60rpx;
	}

	.mdyPricePop-input {
		margin: 10rpx 0 30rpx;

		input {
			border: 1px solid #e6e6e6;
			height: 90rpx;
			border-radius: 10rpx;
			padding: 0 30rpx;
		}

		::v-deep .phcClass {
			color: #ccc;
		}
	}

	.mdyPricePop-tips {
		font-size: 24rpx;
		color: #999;
		display: flex;

		text {
			display: block;
		}
	}

	.mdyPricePop-footer {
		display: flex;
		border-top: 1px solid $g-border-color;
	}

	.mdyPricePop-btn {
		flex: 1;
		line-height: 100rpx;
		text-align: center;

		&:first-child {
			position: relative;
			font-size: 32rpx;

			&::after {
				position: absolute;
				right: 0;
				top: 50%;
				content: '';
				width: 2rpx;
				height: 60rpx;
				transform: translateY(-50%);
				border-radius: 2rpx;
				background-color: $g-main-bg;
			}
		}

		&:last-child {
			font-size: 32rpx;
			color: $g-main-color;
		}
	}
</style>
